﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class PicNewsList : YuanlinUserControl,
        IReplyConverter<NewsCategoryReply, GetListOperation>
    {
        protected DataList dataList;
        protected CurrentPage currentPage;
        private EntityList newsCategories;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!IsPostBack)
            {//找出所有第一层类别的内容
                NewsCategoryListRequest request = new NewsCategoryListRequest();
                request.UpNewsCategoryId = 0;
                requestBinders.Add(RequestHelper.BuildGetNewsCategoryListRequest(this, request));
            }
            dataList.ItemDataBound += new DataListItemEventHandler(dataList_ItemDataBound);
        }
        protected void dataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                NewsCategory newsCategory = e.Item.DataItem as NewsCategory;
                if (newsCategory == null)
                {
                    return;
                }
                NewsListRequest request = new NewsListRequest();
                request.NewsCategoryId = newsCategory.EntityId.Value;
                request.PageIndex = 0;
                request.PageSize = 6;
                request.OrderFields.Add(new OrderField(new News(), "IsTop", false));
                EntityList newsList = BusinessManager.GetNewsList(RequestHelper.BuildGetNewsListRequest(request));
                if (newsList.Count == 0)
                {
                    return;
                }
                News topNews = null;
                EntityList tempNewsList = new EntityList();
                foreach (News item in newsList)
                {
                    if (item.IsTop.Value == 1)
                    {
                        topNews = item;
                    }
                    else if (tempNewsList.Count < 5)
                    {
                        tempNewsList.Add(item);
                    }
                }
                YuanlinImage image = (YuanlinImage)e.Item.FindControl("yuanlinImage");
                HtmlAnchor imageA = (HtmlAnchor)e.Item.FindControl("imageA");
                if (image != null && topNews != null && imageA!=null)
                {
                    image.Attachment = topNews;
                    imageA.HRef = SiteUrls.Instance().GetArticleUrl(topNews.EntityId.Value);
                }
                HyperLink hylImage = (HyperLink)e.Item.FindControl("hylImage");
                if (hylImage != null && topNews != null)
                {
                    hylImage.Text = topNews.Title.Value;
                    hylImage.NavigateUrl = SiteUrls.Instance().GetArticleUrl(topNews.EntityId.Value); 
                }
                Repeater list = (Repeater)e.Item.FindControl("list");
                if (list != null)
                {
                    list.DataSource = tempNewsList;
                    list.DataBind();
                }
            }   

        }

        private void BindData()
        {
            dataList.DataSource = newsCategories;
            dataList.DataBind();
        }

        #region IReplyConverter<NewsCategoryReply,GetListOperation> Members

        public void ConvertReply(NewsCategoryReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                newsCategories = reply.EntityList;

                if (!IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}
